package ldkxingzhe.ldkxingzhedemo.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v7.app.NotificationCompat;

import java.util.HashSet;
import java.util.Set;

import ldkxingzhe.ldkxingzhedemo.R;
import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by liangdengke on 2015/9/11.
 */
public class MessengerService extends Service{
    private final String TAG = MessengerService.class.getSimpleName();

    private NotificationManager mNotificationManager;
    Set<Messenger> mClients = new HashSet<>();
    private Messenger mMessenger = new Messenger(new IncomeHandler());

    private int mValue = 318;

    @Override
    public void onCreate() {
        super.onCreate();
        mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        LogDemo.v(TAG,"Service starting, onCreate");
        showNotification("start service");
    }

    private void showNotification(CharSequence text) {
        Notification notification = new NotificationCompat.Builder(this).setContentTitle("MessageService")
                .setContentText(text)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setTicker(text)
                .setOngoing(true)
                .build();
        mNotificationManager.notify(R.string.service_start,notification);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mMessenger.getBinder();
    }

    final int MSG_REGISTER_CLIENT = 0;
    final int MSG_REMOVE_CLIENT = 1;
    final int MSG_SET_SIMPLE_VALUE= 2;
    final int MSG_SET_COMPLEX_VALUE = 3;
    class IncomeHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MSG_REGISTER_CLIENT:
                    mClients.add(msg.replyTo);
                    LogDemo.v(TAG,"Register clients");
                    break;
                case MSG_REMOVE_CLIENT:
                    mClients.remove(msg.replyTo);
                    LogDemo.v(TAG,"Remove clients");
                    break;
                case MSG_SET_SIMPLE_VALUE:
                    LogDemo.v(TAG,"set simple value");
                    mValue = msg.arg1;
                    LogDemo.v(TAG, "Receive arg1 = " + mValue);
                    showNotification("Receive arg1 = " + mValue);
                    for(Messenger messenger : mClients){
                        try {
                            messenger.send(Message.obtain(null, MSG_SET_SIMPLE_VALUE, mValue, 0));
                        } catch (RemoteException e) {
                            // Connection already dead
                            mClients.remove(msg.replyTo);
                            e.printStackTrace();
                        }
                    }
                    break;
                case MSG_SET_COMPLEX_VALUE:
                    LogDemo.v(TAG,"set complex value");
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onDestroy() {
        LogDemo.v(TAG,"onDestroy");
        mNotificationManager.cancel(R.string.service_start);
        super.onDestroy();
    }
}
